<template>
  <div>
    <BreadCrumb></BreadCrumb>
    <div class="header">
        <div class="form">
          <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="套餐名">
              <el-input v-model="formInline.name" size="small" placeholder="套餐名"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit" size="small">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="group">
          <el-button size="small" type="warning" icon="el-icon-plus" @click="toProductPage">添加套餐</el-button>
          <el-button size="small" type="danger" icon="el-icon-delete" @click="allDelete">批量删除</el-button>
          <!-- <el-button size="small" type="danger" icon="el-icon-delete">批量停售</el-button> -->
        </div>
    </div>
    <div class="content">
      <el-table
      :row-class-name="tableRowClassName"
      :data="tableData"
      @select="selectHandle"
      @select-all="selectHandle"
      style="width: 100%" border :default-sort = "{prop: 'date', order: 'descending'}">
        <el-table-column
        align="center"
        type="selection"
        width="55">
          </el-table-column>
          <el-table-column
        sortable
        align="center"
          prop="name"
          label="套餐名称"
          width="180">
          <template slot-scope="scope">
            <span style="color: blue;cursor: pointer;" @click="handleLook(scope.$index,scope.row)">
              {{ scope.row.name }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
        sortable
        align="center"
          prop="categoryName"
          label="套餐类别"
          width="180">
        </el-table-column>
        <el-table-column
        sortable
        align="center"
          prop="price"
          label="价格/￥"
          width="180">
        </el-table-column>
        <el-table-column
        align="center"
          prop="description"
          label="套餐描述">
        </el-table-column>
        <el-table-column
        sortable
        align="center"
          prop="status"
          label="套餐状态"
          width="180">
          <template slot-scope="scope">
            <span v-if="scope.row.status==0">
              停售
            </span>
            <span v-else>
              起售
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作"  align="center">
        <template slot-scope="scope">
          <el-button
          icon="el-icon-edit"
            size="mini"
            type="primary"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
          icon="el-icon-delete"
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            <el-button
            v-if="scope.row.status==1"
          icon="el-icon-warning"
            size="mini"
            type="warning"
            @click="handleChange(scope.$index, scope.row)">停售</el-button>
            <el-button
            v-else
          icon="el-icon-warning"
            size="mini"
            type="success"
            @click="handleChange(scope.$index, scope.row)">起售</el-button>
        </template>
      </el-table-column>
      </el-table>
        <div class="pagination">
          <Pagination :total="total" :pageSize="pageSize" @CurrentChange="getRecords" :page="page" obj="setmeal"></Pagination>
        </div>
    </div>
  </div>
</template>

<script>
import Pagination  from '@/components/pagination/myPagination.vue';
import axios from 'axios';
// import dayjs from 'dayjs';
export default {
  components:{Pagination},
  data(){
    return{
      total:60,
      pageSize:6,
      page:1,
      ids:'',
      formInline: {
        username:'',
        name: '',
        date: ''
      },
      tableData:[]
    }
  },
  mounted(){
    axios({
      url:`http://localhost:8080/kfc/admin/setmeal/page?page=${this.page}&pageSize=${this.pageSize}`,
      method:'get',
      headers:{
        token:localStorage.getItem('jwt')
      }
    }).then(res=>{
      this.tableData=res.data.data.records
    })
  },
  methods:{
    tableRowClassName({row}) {
      if(row.status==0){
        return 'warning-row'
      }
    },
    handleChange(index, row){
      if(this.tableData[index].status==0){
        this.tableData[index].status=1
      }
      else{
        this.tableData[index].status=0
      }
      this.tableRowClassName
      axios({
        url:`http://localhost:8080/kfc/admin/setmeal/status/edit?id=${row.id}`,
        method:'post',
        headers:{
          token:localStorage.getItem('jwt')
        },
      })
    },
    getRecords(val){
      this.tableData=val
    },
    allDelete(){
      axios({
        url:`http://localhost:8080/kfc/admin/setmeal/delete?ids=${this.ids}`,
        method:'delete',
        headers:{
          token:localStorage.getItem('jwt')
        }
      }).then(res=>{
        if(res.status==200){
          this.$message({
              type:'success',
              message:'删除成功'
          })
          axios({
            url:`http://localhost:8080/kfc/admin/setmeal/page?page=${this.page}&pageSize=${this.pageSize}`,
            method:'get',
            headers:{
              token:localStorage.getItem('jwt')
            }
          }).then(res=>{
            console.log(res.data.data.records);
            this.tableData=res.data.data.records
          })
        }
        else{
          this.$message.error('删除失败');
        }
      })
    },
    onSubmit() {
      axios({
      url:`http://localhost:8080/kfc/admin/setmeal/page?page=${this.page}&pageSize=${this.pageSize}&name=${this.formInline.name}`,
      method:'get',
      headers:{
        token:localStorage.getItem('jwt')
      },
    }).then(res=>{
      this.tableData=res.data.data.records
    })
      console.log('submit!', this.formInline.name);
    },
    selectHandle(selection){
      console.log(selection);
      for(let i=0;i<selection.length;i++){
        if(i==0){
          this.ids+=selection[i].id
        }
        else{
          this.ids+=','+selection[i].id
        }
      }
    },
    handleEdit(index, row) {
        console.log(index, row);
        // 跳转编辑页面
        this.$router.push({
          name:'userPage',
          params:{
            type:'update',
            id:row.id
          }
        })
      },
    handleDelete(index, row) {
      console.log(index, row);
      axios({
        url:`http://localhost:8080/kfc/admin/setmeal/delete?ids=${row.id}`,
        method:'delete',
        headers:{
          token:localStorage.getItem('jwt')
        }
      }).then(res=>{
        if(res.status==200){
          this.$message({
              type:'success',
              message:'删除成功'
          })
          axios({
            url:`http://localhost:8080/kfc/admin/setmeal/page?page=${this.page}&pageSize=${this.pageSize}`,
            method:'get',
            headers:{
              token:localStorage.getItem('jwt')
            }
          }).then(res=>{
            console.log(res.data.data.records);
            this.tableData=res.data.data.records
          })
        }
        else{
          this.$message.error('删除失败');
        }
      })
    },
    toProductPage(){
      this.$router.push({
        name:'userPage',
        params:{
          type:'add'
        }
      })
    },
    // 查看商品详情
    handleLook(index,row){
      console.log(index, row);
      this.$router.push({
        name:'userPage',
        params:{
          type:'look',
          id:row.id
        }
      })
    }
  }
}
</script>

<style>
  .el-table .warning-row {
    background: oldlace;
  }
</style>

<style lang="less" scoped>
  .header{
    background: #FFF;
    margin-bottom: 20px;
    padding: 10px;
    .el-form-item{
      margin-bottom: 16px;
    }
    .group{
      border: 1px solid #eee;
      padding: 8px;
    }
  }
  .content{
    background: #FFF;
    .pagination{
      padding: 10px;
    }
  }
</style>